package com.vessel.decoration.mvp.ui.adapter;

import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.lx.ratingbarlib.MyRatingBar;
import com.vessel.decoration.BuildConfig;
import com.vessel.decoration.R;
import com.vessel.decoration.app.data.entity.CollectResponse;

import java.util.List;

public class CollectAdapter extends BaseMultiItemQuickAdapter<CollectResponse.CollectsBean, BaseViewHolder> {

    private AppComponent mAppComponent;

    private static final int MODE_CHECK = 0;
    int mEditMode = MODE_CHECK;

    public CollectAdapter(List<CollectResponse.CollectsBean> data) {
        super(data);
        addItemType(CollectResponse.CollectsBean.TYPE_COLLECT_ARTISAN, R.layout.item_collect);
        addItemType(CollectResponse.CollectsBean.TYPE_COLLECT_SHOP, R.layout.item_collect);
    }

    @Override
    protected void convert(BaseViewHolder helper, CollectResponse.CollectsBean item) {
        if (mAppComponent == null) {
            mAppComponent = ArmsUtils.obtainAppComponentFromContext(mContext);
        }
        if (mEditMode == MODE_CHECK) {
            helper.getView(R.id.shop_check).setVisibility(View.GONE);
        } else {
            helper.getView(R.id.shop_check).setVisibility(View.VISIBLE);
            ((CheckBox)helper.getView(R.id.shop_check)).setOnCheckedChangeListener((compoundButton, b) -> item.setSelect(b));
            if (item.isSelect()) {
                ((CheckBox)helper.getView(R.id.shop_check)).setChecked(true);
            } else {
                ((CheckBox)helper.getView(R.id.shop_check)).setChecked(false);
            }
        }
        switch (item.getItemType()) {
            case CollectResponse.CollectsBean.TYPE_COLLECT_ARTISAN:
                mAppComponent.imageLoader().loadImage(mContext, ImageConfigImpl.builder().imageView(helper.getView(R.id.shop_ic)).url(BuildConfig.APP_DOMAIN + item.getArtisan().getRealPhoto()).build());
                ((TextView)helper.getView(R.id.shop_name)).setText(item.getArtisan().getRealName());
                ((TextView)helper.getView(R.id.shop_tips)).setText("技工");
                ((MyRatingBar)helper.getView(R.id.score)).setRating(item.getArtisan().getScore());
                break;
            case CollectResponse.CollectsBean.TYPE_COLLECT_SHOP:
                mAppComponent.imageLoader().loadImage(mContext, ImageConfigImpl.builder().imageView(helper.getView(R.id.shop_ic)).url(BuildConfig.APP_DOMAIN + item.getShop().getShopPic()).build());
                ((TextView)helper.getView(R.id.shop_name)).setText(item.getShop().getShopName());
                ((TextView)helper.getView(R.id.shop_tips)).setText("建材");
                ((MyRatingBar)helper.getView(R.id.score)).setRating(item.getShop().getScore());
                break;
        }
    }

    public void setEditMode(int editMode) {
        mEditMode = editMode;
        notifyDataSetChanged();
    }
}
